package com.example.shixun1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.shixun1.R;
import com.google.android.material.textfield.TextInputEditText;

public class ChangePasswordActivity extends AppCompatActivity {
    private TextInputEditText etNewPassword;
    private int position;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        position = getIntent().getIntExtra("position", -1);
        username = getIntent().getStringExtra("username");

        etNewPassword = findViewById(R.id.et_new_password);

        findViewById(R.id.btn_confirm).setOnClickListener(v -> {
            String newPassword = etNewPassword.getText().toString();
            if (validatePassword(newPassword)) {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("position", position);
                resultIntent.putExtra("new_password", newPassword);
                setResult(RESULT_OK, resultIntent);
                finish();
            }
        });
    }

    private boolean validatePassword(String password) {
        if (password.isEmpty()) {
            showError("密码不能为空");
            return false;
        }
        if (password.length() < 6) {
            showError("密码至少需要6位");
            return false;
        }
        return true;
    }

    private void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}